/**
A multi shaped mine sweeper game
SDLHexamine  Copyright (C) 2009-2010  Andematunum-Software
(andematunum-software@googlegroups.com)

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "board.h"
#include "resources.h"
#include "SDL.h"
#include <string.h>

extern SDL_Surface* screen;

MineBoard* MineBoard::instance = NULL;

void MineBoard::
allocate_cells (Uint16 width, Uint16 height) {
  cells = new CellState[width*height];
  if (cells == NULL) {
    char* msg = new char[64];
    sprintf(msg,"Cannot allocate memory for a %ux%u mine board.",
            width, height);
    throw msg;
  }
  // Remember the width for 2d->1d coordinate conversions.
  this->width = width;
#ifdef DEBUG_MineBoard
  cout << "*** DEBUG: Allocated " << width << " x " << height
       << " cells." << endl;
#endif
}

void build_board (int number_of_rings) {
  int cx = (screen->w - 20*TILE_W) / 2.;
  int cy = (screen->h - 10*TILE_H) / 2.;

  SDL_Rect src = {0, 0, TILE_W, TILE_H};
  SDL_Rect dst = {0, 0, TILE_W, TILE_H};
  SDL_Surface* tile = Resources::get_instance()->get("covered");

  /* for (int n = 0; n < number_of_rings; n++) {

     }*/
  for (int j = 0; j < 10; j++) {
    for (int i = 0; i < 20; i++) {
      dst.x = i*XOFFSET;
      dst.y = (i % 2) ? (j*TILE_H+YOFFSET):(j*TILE_H);
      dst.x += cx;
      dst.y += cy;
      SDL_BlitSurface(tile,&src,screen,&dst);
    }
  }

  SDL_Flip(screen);
}

/* Ici, j'ai juste commenté pour que tu adaptes
   à la nouvelle interface, sachant que l'initialisation
   de la mémoire et l'accès se font maintenant grâce
   à des fonctions de MineBoard, beaucoup plus sûres
   qu'un accès direct au tableau.
*/
/*
HexaBoard::init( unsigned int rings ){

  // table creation
  width_ = 1 + 2 * rings;
  height_ = width_;

  nb_cells_ = 1;
  for( int i = 1; i<=rings; ++i ){
    nb_cells_ += 6*i;
  }

  if( mine_table_ != NULL ){
    delete[] mine_table_;
  }
  mine_table_ = new CellState[ width_*height_ ]; 

  // table filling
  for( int i= 0; i< width_; ++i ){
    for( int j= 0; j< width_; ++j ){

      *mine_table_[ i * width_ + j ] = Free;
    }
  }
*/

